package jp.eisbahn.mgapi.http;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletResponse;

public class HttpUtils {
    
    public static boolean isSuccess(HttpResponse response) {
        int responseCode = response.getResponseCode();
        return responseCode == HttpServletResponse.SC_OK
            || responseCode == HttpServletResponse.SC_CREATED;
    }
    
    public static String createQueryParameterString(Map<String, String> parameterMap) {
        try {
            StringBuilder sb = new StringBuilder();
            boolean first = true;
            for (Entry<String, String> entry : parameterMap.entrySet()) {
                if (first) {
                    first = false;
                } else {
                    sb.append("&");
                }
                sb.append(entry.getKey());
                sb.append("=");
                sb.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
            }
            return sb.toString();
        } catch(UnsupportedEncodingException e) {
            throw new IllegalStateException(e);
        }
    }

}
